import time
from datetime import datetime, timedelta

import requests


url = "https://app.bilibili.com/x/wall/unicom/order/pack/receive"

headers = {
    "Accept": "*/*",
    "Accept-Language": "zh,zh-TW;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "你的cookie",
    "Origin": "https://www.bilibili.com",
    "Referer": "https://www.bilibili.com/blackboard/activity-new-freedata.html",
    "Sec-Ch-Ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
    "Sec-Ch-Ua-Mobile": "?1",
    "Sec-Ch-Ua-Platform": "\"Android\"",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36",
}

# 请求参数
data = {
    # 根据实际情况添加请求参数
    "cross_domain":"true",
    "id":"3",
    "csrf":"1ee17dea5b7a252c233098871d165bab"
}



def getDaHuiYuan():
    response = requests.post(url, headers=headers, data=data)
    # 在访问 response.text 之前设置字符编码
    binary_data = response.content

    # encoded_content = gzip.decompress(binary_data).decode("utf-8")

    # 获取压缩后的内容
    raw_data = response.content
    # 尝试不同的解码方式
    try:
        decoded_response = raw_data.decode('utf-8')
    except UnicodeDecodeError:
        try:
            decoded_response = raw_data.decode('latin-1')
        except UnicodeDecodeError:
            try:
                decoded_response = raw_data.decode('utf-16')
            except UnicodeDecodeError:
                # 添加其他可能的解码方式
                decoded_response = "无法解码"

    print(response.status_code)
    print(response.text)
    if(response.status_code == 200):
        return "请求成功！",response.text
    else:
        return "请求失败！",response.text


def handler(environ, start_response):
    resout = getDaHuiYuan()
    print(resout)
    return resout

def execute_code(time_day):
    # 这里替换成你要执行的代码
    print(f"{time_day}号，代码执行中...")
    while True:
        # 获取当前时间
        now = datetime.now()
        nowDay = now.day
        nowSecond = now.second
        print(f"{time_day}号，现在时间",nowDay,nowSecond)
        if nowSecond>58 or nowSecond <= 5 :
            print(f"抢购中...")
        #     不等于昨天，且时间小于5秒
            time.sleep(0.01)
            resout = getDaHuiYuan()
            print(resout)
        else:
            a =True
            break

global  a
a = False;

if __name__ == "__main__":
    resout = getDaHuiYuan()
    print(resout)

    # while True:
    #
    #     if a:
    #         break
    #
    #     # 获取当前时间
    #     now = datetime.now()
    #
    #     # 获取当前时间的小时和分钟
    #     current_hour = now.hour
    #     current_minute = now.minute
    #     current_c = now.second
    #     print(current_hour,current_minute,current_c)
    #
    #     # 判断是否是23:59
    #     if current_hour == 23 and current_minute == 59:
    #         print("现在是23:59分以后")
    #         while True:
    #
    #             if a:
    #                 break
    #
    #             # 获取当前时间
    #             nows = datetime.now()
    #             current_second = nows.second
    #             print("current_second",current_second)
    #             if current_second>=50:
    #                 print("现在大于50秒后")
    #                 while True:
    #                     # 获取当前时间
    #                     nows = datetime.now()
    #                     current_second = nows.second
    #                     if current_second >= 58:
    #                         print("现在大于58秒后")
    #                         execute_code(nows.day)
    #                         break
    #                     else:
    #                         print("50-58之间")
    #                         time.sleep(0.2)
    #             else:
    #                 print("现在50秒qian")
    #                 print("0-50之间")
    #                 if a:
    #                     break
    #                 time.sleep(5)
    #     else:
    #         time.sleep(60)
    #         print("现在不是23:59")

